/*
 * @Author: liuxinyea
 * @Date: 2024-08-08 22:29:46
 * @LastEditors: liuxinyea
 * @LastEditTime: 2024-08-11 21:14:44
 * @Description: 描述
 */
import { _decorator, CCInteger, CCString, Component, Enum, EventMouse, Node, Sprite } from 'cc';
import { SunManager } from './manager/SunManager';
import { MouseManager } from './manager/MouseManager';
import { CardState, PlantType } from './Glabol';
const { ccclass, property } = _decorator;

@ccclass('Card')
export class Card extends Component {
    private cardState: CardState = CardState.Cooling; //卡牌状态
    @property(Node)
    public cardLight: Node = null; //卡牌可用的时候
    @property(Node)
    public cardGray: Node = null; //卡牌不可用的时候
    @property(Sprite)
    public cardMask: Sprite = null; //卡牌遮罩
    @property({ type: Enum(PlantType), tooltip: "植物类型" })
    public plantType: PlantType = PlantType.Sunflower;
    
    public cdTime: number = 2; //卡牌冷却时间
    private cdTimer: number = 0; //卡牌冷却计时器
    @property({type:CCInteger,tooltip:"需要多少阳光"})
    private needSunPoint: number = 0;

    start() {
       this.cdTimer = 2;
    }

    update(deltaTime: number) {
        switch (this.cardState) {
            case CardState.Cooling:
                this.CoolingUpdate(deltaTime)
                break;
            case CardState.WaitingSun:
                this.WaitingSunUpdate()
                break;
            case CardState.Ready:
                this.ReadyUpdate()
               break;
            default:
                break;
        }
    }
    private CoolingUpdate(dt:number) {
       this.cdTimer -= dt
       this.cardMask.fillRange = -(this.cdTimer / this.cdTime)
       if(this.cdTimer <= 0){
          this.transitionWaitingSun()
       }
    }
    private transitionWaitingSun() {
        this.cardState = CardState.WaitingSun;
        this.cardLight.active = false;
        this.cardGray.active = true;
        this.cardMask.node.active = false;
    }
    private WaitingSunUpdate() {
        // console.log('WaitingSunUpdate:', SunManager.instance.SunPoint);
        if (this.needSunPoint<= SunManager.instance.SunPoint){
            this.transitionReady()
        }
    }

    private transitionReady() {
        this.cardState = CardState.Ready;
        this.cardLight.active = true;
        this.cardGray.active = false;
    }

    private ReadyUpdate() {
        // console.log('ReadyUpdate:', SunManager.instance.SunPoint);
        
        if (this.needSunPoint > SunManager.instance.SunPoint) {
            this.transitionWaitingSun()
        }
    }
    async onClick(event:EventMouse){
        if(this.needSunPoint > SunManager.instance.SunPoint) return;
        // TODO 点击触发种植植物        
        const isSuccess = await MouseManager.instance.addPlant(this.plantType, event)
        if(!isSuccess){
            console.log('种植失败');
            return
        }
        SunManager.instance.subPoint(this.needSunPoint)
        this.transitionCooling()
    }
    private transitionCooling() {
        this.cardState = CardState.Cooling;
        this.cardLight.active = false;
        this.cardGray.active = true;
        this.cardMask.node.active = true;
        this.cdTimer = this.cdTime;
    }
}



